package log

import (
	"io"
	"jiance_server/config"
	slog "log"
	"os"
	"path"

	dylog "github.com/cdongyang/library/log"
	"github.com/cdongyang/library/utils"
)

var conf struct {
	File     string
	Level    int
	ToStdout bool
}
var defautlLogger *dylog.Logger

var Panicln func(args ...interface{})
var Panic func(args ...interface{})
var Panicf func(format string, args ...interface{})
var Debugln func(args ...interface{})
var Debug func(args ...interface{})
var Debugf func(format string, args ...interface{})
var Logln func(args ...interface{})
var Log func(args ...interface{})
var Logf func(format string, args ...interface{})
var Noticeln func(args ...interface{})
var Notice func(args ...interface{})
var Noticef func(format string, args ...interface{})
var Warnln func(args ...interface{})
var Warn func(args ...interface{})
var Warnf func(format string, args ...interface{})
var Errorln func(args ...interface{})
var Error func(args ...interface{})
var Errorf func(format string, args ...interface{})

func init() {
	utils.PanicError(utils.LoadCurrentDirJSONFile("config.json", &conf))
	file, err := os.OpenFile(path.Join(config.GetConfig().Path, conf.File), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
	utils.PanicError(err)
	var mw io.Writer
	if conf.ToStdout {
		mw = io.MultiWriter(file, os.Stdout)
	} else {
		mw = file
	}
	defautlLogger = dylog.New(slog.New(mw, "", slog.LstdFlags|slog.Lshortfile), 4, conf.Level)
	Panicln = defautlLogger.Panicln
	Panic = defautlLogger.Panic
	Panicf = defautlLogger.Panicf
	Debugln = defautlLogger.Debugln
	Debug = defautlLogger.Debug
	Debugf = defautlLogger.Debugf
	Logln = defautlLogger.Logln
	Log = defautlLogger.Log
	Logf = defautlLogger.Logf
	Noticeln = defautlLogger.Noticeln
	Notice = defautlLogger.Notice
	Noticef = defautlLogger.Noticef
	Warnln = defautlLogger.Warnln
	Warn = defautlLogger.Warn
	Warnf = defautlLogger.Warnf
	Errorln = defautlLogger.Errorln
	Error = defautlLogger.Error
	Errorf = defautlLogger.Errorf
}
